package com.ydc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ydc.mapper.RightsMapper;
import com.ydc.mapper.RightsUserMapper;
import com.ydc.pojo.Rights;
import com.ydc.pojo.RightsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RightsServiceImpl implements RightsService {

    @Autowired
    private RightsMapper rightsMapper;
    @Autowired
    private RightsUserMapper rightsUserMapper;


    @Override
    public List<Rights> findRightsList() {

        //1.查询一级菜单数据
        QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<Rights> oneList = rightsMapper.selectList(queryWrapper);
        //2.查询二级菜单数据  需要封装二级set给一级
        for (Rights oneRights : oneList) {
            QueryWrapper<Rights> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("parent_id",oneRights.getId());
            List<Rights> twoList = rightsMapper.selectList(queryWrapper2);
            oneRights.setChildren(twoList);
        }
        return oneList;
    }

    @Override
    public List<RightsUser> getRightsUserList() {
        //1.查询一级菜单数据
        QueryWrapper<RightsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<RightsUser> oneList = rightsUserMapper.selectList(queryWrapper);
        //2.查询二级菜单数据  需要封装二级set给一级
        for (RightsUser oneRights : oneList) {
            QueryWrapper<RightsUser> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("parent_id",oneRights.getId());
            List<RightsUser> twoList = rightsUserMapper.selectList(queryWrapper2);
            oneRights.setChildren(twoList);
        }
        return oneList;
    }
}
